load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "opt",
    srcs = [
        "colset.go",
        "column_meta.go",
        "constants.go",
        "doc.go",
        "locking.go",
        "metadata.go",
        "operator.go",
        "ordering.go",
        "panic_injection.go",
        "rule_name.go",
        "schema_dependencies.go",
        "table_meta.go",
        "telemetry.go",
        "values.go",
        ":gen-operator",  # keep
        ":gen-rulenames",  # keep
        ":gen-rulenames-stringer",  # keep
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/server/telemetry",
        "//pkg/sql/catalog",
        "//pkg/sql/catalog/catpb",
        "//pkg/sql/catalog/colinfo",
        "//pkg/sql/catalog/descpb",
        "//pkg/sql/catalog/multiregion",
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/partition",
        "//pkg/sql/pgwire/pgcode",
        "//pkg/sql/pgwire/pgerror",
        "//pkg/sql/privilege",
        "//pkg/sql/sem/catid",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/sem/tree/treebin",
        "//pkg/sql/sem/tree/treecmp",
        "//pkg/sql/sqltelemetry",
        "//pkg/sql/types",
        "//pkg/util",
        "//pkg/util/buildutil",
        "//pkg/util/intsets",
        "//pkg/util/log",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",
        "@com_github_lib_pq//oid",
    ],
)

go_test(
    name = "opt_test",
    size = "small",
    srcs = [
        "colset_test.go",
        "main_test.go",
        "metadata_test.go",
        "operator_agg_test.go",
        "operator_test.go",
        "ordering_test.go",
    ],
    data = [
        "//c-deps:libgeos",
    ],
    embed = [":opt"],
    deps = [
        "//pkg/base",
        "//pkg/roachpb",
        "//pkg/security/securityassets",
        "//pkg/security/securitytest",
        "//pkg/server",
        "//pkg/settings/cluster",
        "//pkg/sql/catalog/descpb",
        "//pkg/sql/opt/cat",
        "//pkg/sql/opt/memo",
        "//pkg/sql/opt/norm",
        "//pkg/sql/opt/props",
        "//pkg/sql/opt/testutils/testcat",
        "//pkg/sql/privilege",
        "//pkg/sql/randgen",
        "//pkg/sql/sem/catid",
        "//pkg/sql/sem/eval",
        "//pkg/sql/sem/tree",
        "//pkg/sql/types",
        "//pkg/testutils/serverutils",
        "//pkg/testutils/sqlutils",
        "//pkg/util/intsets",
        "//pkg/util/leaktest",
        "//pkg/util/log",
        "//pkg/util/randutil",
        "@com_github_stretchr_testify//require",
    ],
)

# Define a file group for all the operator definitions.
ops = filegroup(
    name = "ops",
    srcs = glob(["ops/*.opt"]),
    visibility = ["//visibility:public"],
)

# Define the generator for the operator definitions and functions.
genrule(
    name = "gen-operator",
    srcs = [":ops"],
    outs = ["operator.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/optgen) -out $@ ops $(locations :ops)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/optgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

# Define the generator for the enumeration of rulenames.
genrule(
    name = "gen-rulenames",
    srcs = [
        ":ops",
        "//pkg/sql/opt/xform:rules",
        "//pkg/sql/opt/norm:rules",
    ],
    outs = ["rule_name.og.go"],
    cmd = """
      $(location //pkg/sql/opt/optgen/cmd/optgen) -out $@ rulenames \
          $(locations :ops) $(locations //pkg/sql/opt/norm:rules) $(locations //pkg/sql/opt/xform:rules)
    """,
    tools = ["//pkg/sql/opt/optgen/cmd/optgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

# Define the generator for the stringification of rulenames.
#
# NB: Stringer doesn't support running stringer across files in multiple
# directories. To work around this, we manually copy one of the source files
# over into the location of the other, and run stringer there instead. Ew.
genrule(
    name = "gen-rulenames-stringer",
    srcs = [
        "rule_name.go",
        ":gen-rulenames",
    ],
    outs = ["rule_name_string.go"],
    cmd = """
      cp $(location rule_name.go) `dirname $(location :gen-rulenames)`/rule_name.go
      $(location //pkg/build/bazel/util/tinystringer) -output=$@ \
          -type=RuleName `dirname $(location :gen-rulenames)`/rule_name.go $(location :gen-rulenames)
    """,
    tools = [
        "//pkg/build/bazel/util/tinystringer",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)
